<template>
  <div
      class="diy_edit registered_user_edits page_registered_user"
      id="registered_user_edit"
  >
    <el-form
        :model="form"
        ref="ruleFormRef" 
		:rules="rules"
        label-width="auto"
        class="edit_registered_user_form"
        size="large"
    >
      <el-row class="row_e">
        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="头像" prop="avatar">
            <el-upload class="avatar-uploader" drag accept="image/jpeg, image/png, image/jpg"
                action="" :http-request="uploadUserAvatar" :show-file-list="false">
              <img v-if="form_user.avatar" :src="$fullUrl($banseUrl, form_user.avatar)" class="avatar"/>
              <el-icon v-else><Plus/></el-icon>
            </el-upload>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="账号" prop="username">
            <span v-if="obj_user.username">{{ obj_user.username }}</span>
            <el-input v-else v-model="form_user.username" placeholder="请输入用户名" autocomplete="new-password"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form_user.nickname" placeholder="请输入昵称"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item v-if="!obj_user.password" label="密码" prop="password">
            <span v-if="obj_user.password">{{ obj_user.password }}</span>
            <el-input v-else type="password" v-model="form_user.password" placeholder="请输入密码" show-password autocomplete="new-password"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="邮箱" prop="email">
            <el-input type="email" v-model="form_user.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="状态" prop="state">
            <el-select v-model="form_user.state" placeholder="请选择">
              <el-option v-for="group in list_user_state" :key="group.value" :label="group.name" :value="group.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
			<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="会员等级" prop="vip_level">
				<el-select :disabled="user_group!=='管理员'" v-model="form_user.vip_level" placeholder="请选择" @change="vip_level_change">
					<el-option v-for="vip_level in list_user_vip_level" :key="vip_level.name" :label="vip_level.name"
							   :value="vip_level.name">
					</el-option>
				</el-select>
			</el-form-item>
		</el-col>
		<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="会员折扣" prop="vip_discount">
				<el-input :disabled="true" v-model="form_user.vip_discount" placeholder="请输入会员折扣"></el-input>
			</el-form-item>
		</el-col>
	          <el-col v-if="$check_field('ger','membership_card_number','/registered_user/view') || $check_field('add','membership_card_number','/registered_user/view') || $check_field('set','membership_card_number','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="会员卡号" prop="membership_card_number">
                      <el-input id="membership_card_number" v-model="form['membership_card_number']" placeholder="请输入会员卡号"
                      v-if="(form['registered_user_id'] && $check_field('set','membership_card_number','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','membership_card_number','/registered_user/view'))" :disabled="disabledObj['membership_card_number_isDisabled']"></el-input>
            <div v-else-if="$check_field('ger','membership_card_number','/registered_user/view')">{{form['membership_card_number']}}</div>
                    </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','real_name','/registered_user/view') || $check_field('add','real_name','/registered_user/view') || $check_field('set','real_name','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="真实姓名" prop="real_name">
                      <el-input id="real_name" v-model="form['real_name']" placeholder="请输入真实姓名"
                      v-if="(form['registered_user_id'] && $check_field('set','real_name','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','real_name','/registered_user/view'))" :disabled="disabledObj['real_name_isDisabled']"></el-input>
            <div v-else-if="$check_field('ger','real_name','/registered_user/view')">{{form['real_name']}}</div>
                    </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_gender','/registered_user/view') || $check_field('add','user_gender','/registered_user/view') || $check_field('set','user_gender','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户性别" prop="user_gender">
                <el-select id="user_gender" v-model="form['user_gender']"
                       v-if="(form['registered_user_id'] && $check_field('set','user_gender','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_gender','/registered_user/view'))">
              <el-option v-for="o in list_user_gender" :key="o" :label="o" :value="o">
              </el-option>
            </el-select>
            <div v-else-if="$check_field('ger','user_gender','/registered_user/view')">{{form['user_gender']}}</div>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_age','/registered_user/view') || $check_field('add','user_age','/registered_user/view') || $check_field('set','user_age','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户年龄" prop="user_age">
                <el-input-number id="user_age" v-model.number="form['user_age']"
                             v-if="(form['registered_user_id'] && $check_field('set','user_age','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_age','/registered_user/view'))" :disabled="disabledObj['user_age_isDisabled']"></el-input-number>
            <div v-else-if="$check_field('ger','user_age','/registered_user/view')">{{form['user_age']}}</div>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_tags','/registered_user/view') || $check_field('add','user_tags','/registered_user/view') || $check_field('set','user_tags','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户标签" prop="user_tags">
                <el-select id="user_tags" v-model="user_tags_multiple_value" multiple @change="select_user_tags_multiple"
                       v-if="(form['registered_user_id'] && $check_field('set','user_tags','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_tags','/registered_user/view'))">
              <el-option v-for="o in list_user_tags" :key="o['classification_name']" :label="o['classification_name']"
                         :value="o['classification_name']">
              </el-option>
            </el-select>
            <div v-else-if="$check_field('ger','user_tags','/registered_user/view')">{{form['user_tags']}}</div>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_preferences','/registered_user/view') || $check_field('add','user_preferences','/registered_user/view') || $check_field('set','user_preferences','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户喜好" prop="user_preferences">
                <el-select id="user_preferences" v-model="form['user_preferences']" @change="select_user_preferences_field"                       v-if="(form['registered_user_id'] && $check_field('set','user_preferences','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_preferences','/registered_user/view'))">
              <el-option v-for="o in list_user_preferences" :key="o['classification_name']" :label="o['classification_name']"
                         :value="o['classification_name']">
              </el-option>
            </el-select>
            <div v-else-if="$check_field('ger','user_preferences','/registered_user/view')">{{form['user_preferences']}}</div>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','description_and_explanation','/registered_user/view') || $check_field('add','description_and_explanation','/registered_user/view') || $check_field('set','description_and_explanation','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="描述说明" prop="description_and_explanation">
                <el-input id="description_and_explanation" v-model="form['description_and_explanation']"
                      v-if="(form['registered_user_id'] && $check_field('set','description_and_explanation','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','description_and_explanation','/registered_user/view'))" :disabled="true"></el-input>
            <div v-else-if="$check_field('ger','description_and_explanation','/registered_user/view')">{{form['description_and_explanation']}}</div>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','document_information','/registered_user/view') || $check_field('add','document_information','/registered_user/view') || $check_field('set','document_information','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="证件信息" prop="document_information">
                      <el-input id="document_information" v-model="form['document_information']" placeholder="请输入证件信息"
                      v-if="(form['registered_user_id'] && $check_field('set','document_information','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','document_information','/registered_user/view'))" :disabled="disabledObj['document_information_isDisabled']"></el-input>
            <div v-else-if="$check_field('ger','document_information','/registered_user/view')">{{form['document_information']}}</div>
                    </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_photo','/registered_user/view') || $check_field('add','user_photo','/registered_user/view') || $check_field('set','user_photo','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户照片" prop="user_photo">
                <el-upload :disabled="disabledObj['user_photo_isDisabled']" class="avatar-uploader" drag
                       accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_user_photo"
                       :show-file-list="false" v-if="(form['registered_user_id'] && $check_field('set','user_photo','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_photo','/registered_user/view'))">
              <img id="user_photo" v-if="form['user_photo']" :src="$fullUrl($banseUrl,form['user_photo'])" class="avatar">
              <el-icon v-else><Plus /></el-icon>
            </el-upload>
            <el-image v-else-if="$check_field('ger','user_photo','/registered_user/view')" style="width: 100px; height: 100px"
                      :src="$fullUrl(form['user_photo'])" :preview-src-list="[$fullUrl(form['user_photo'])]">
              <div slot="error" class="image-slot">
                <img src="../../assets/images/user.png" style="width: 90px; height: 90px" />
              </div>
            </el-image>
              </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','personal_works','/registered_user/view') || $check_field('add','personal_works','/registered_user/view') || $check_field('set','personal_works','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="个人作品" prop="personal_works">
                      <el-input id="personal_works" v-model="form['personal_works']" placeholder="请输入个人作品"
                      v-if="(form['registered_user_id'] && $check_field('set','personal_works','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','personal_works','/registered_user/view'))" :disabled="disabledObj['personal_works_isDisabled']"></el-input>
            <div v-else-if="$check_field('ger','personal_works','/registered_user/view')">{{form['personal_works']}}</div>
                    </el-form-item>
        </el-col>
            <el-col v-if="$check_field('ger','user_remarks','/registered_user/view') || $check_field('add','user_remarks','/registered_user/view') || $check_field('set','user_remarks','/registered_user/view')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
    <el-form-item label="用户备注" prop="user_remarks">
                <el-input type="textarea" id="user_remarks" v-model="form['user_remarks']" placeholder="请输入用户备注"
                      v-if="(form['registered_user_id'] && $check_field('set','user_remarks','/registered_user/view')) || (!form['registered_user_id'] && $check_field('add','user_remarks','/registered_user/view'))" :disabled="disabledObj['user_remarks_isDisabled']"></el-input>
            <div v-else-if="$check_field('ger','user_remarks','/registered_user/view')">{{form['user_remarks']}}</div>
              </el-form-item>
        </el-col>
        </el-row>
      <div v-if="$check_action('/registered_user/view','set') || $check_action('/registered_user/view','add') || $check_option('/registered_user/table','examine')" class="footer">
        <el-button type="primary" @click="onSubmit" size="large">提交</el-button>
        <el-button @click="cancel()" size="large">取消</el-button>
      </div>
      <div v-else class="footer">
        <el-button @click="cancel()" size="large">取消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
  import {ElMessage} from "element-plus";
  import {ref, onMounted} from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import {useStore} from "vuex";
  import { useRouter, useRoute } from "vue-router";
  import {
          $check_field,
          $fullUrl,
          $banseUrl,
          $check_action,
          $getUrlParams,
          filterSensitiveWords,
          $get_stamp,
          $get_rand,
          $toTime,
          $push
  } from "@/utils/utils.js";

  let router = useRouter();
  let route = useRoute();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  let user_id = storageHelper.getUserInfo().user_id;
  let user_group = storageHelper.getUserInfo().user_group;
  let registered_user_id = $getUrlParams().registered_user_id;
  let url_add = ref("/api/registered_user/add");
  let url_set = ref("/api/registered_user/set");
  let field = ref("registered_user_id")
  let url_get_obj = ref("/api/registered_user/get_obj");
  let url_upload = ref("/api/registered_user/upload");
  let ruleFormRef = ref(null);
  
  let rules = ref({
											  });

  let disabledObj = ref({
      "membership_card_number_isDisabled": false,
        "real_name_isDisabled": false,
        "user_gender_isDisabled": false,
        "user_age_isDisabled": false,
        "user_tags_isDisabled": false,
        "user_preferences_isDisabled": false,
        "description_and_explanation_isDisabled": false,
        "document_information_isDisabled": false,
        "user_photo_isDisabled": false,
        "personal_works_isDisabled": false,
        "user_remarks_isDisabled": false,
    });
    let form_user = ref({
        user_id: 0,
        username: "",
        nickname: "",
        password: "",
        avatar: "",
        email: "",
        state: 1,
	        vip_level: "",
        vip_discount: 0,
			user_group: "注册用户",
    });
    let obj_user = ref({
      user_id: user_id,
      username: "",
      nickname: "",
      password: "",
      avatar: "",
      email: "",
      user_group: "注册用户",
      state: 1,
	      vip_level: "",
      vip_discount: 0,
	    });
        let list_user_vip_level = ref([]);
        let is_password = ref(true);
    let group_table = ref("registered_user");
    // 账号状态
    let list_user_state = ref([
      {
        value: 1,
        name: "可用",
      },
      {
        value: 2,
        name: "异常",
      },
      {
        value: 3,
        name: "已冻结",
      },
      {
        value: 4,
        name: "已注销",
      },
    ]);
  let form = ref({
    "membership_card_number":  '', // 会员卡号
      "real_name":  '', // 真实姓名
      "user_gender":  '', // 用户性别
      "user_age":  0, // 用户年龄
      "user_tags":  '', // 用户标签
      "user_preferences":  '', // 用户喜好
      "description_and_explanation":  '', // 描述说明
      "document_information":  '', // 证件信息
      "user_photo":  '', // 用户照片
      "personal_works":  '', // 个人作品
      "user_remarks":  '', // 用户备注
      "registered_user_id": 0, // ID
  });
  
    
            // 用户性别选项列表
  let list_user_gender = ref(['男','女']);
  
    
            let user_tags_multiple_value = ref([]);
        // 用户标签选项列表
  let list_user_tags = ref([""]);
  
            // 用户喜好选项列表
  let list_user_preferences = ref([""]);
  
    
    
    
    
    
                                                                                                  //获取标签列表
    let get_list_user_tags = async () => {
      let json = await $get("/api/classification_information/get_list");
      if (json.result && json.result.list) {
        list_user_tags.value = json.result.list;
      } else if (json.error) {
        console.error(json.error);
      }
    }
                    let select_user_tags_multiple = (v) => {
      form.value.user_tags = "";
      if (v && v.length > 0) {
        form.value.user_tags = v.toString();
      }
    };
                                    //获取标签列表
    let get_list_user_preferences = async () => {
      let json = await $get("/api/classification_information/get_list");
      if (json.result && json.result.list) {
        list_user_preferences.value = json.result.list;
      } else if (json.error) {
        console.error(json.error);
      }
    }
            //获取用户喜好列表
    let select_user_preferences_field = async (v) => {
      await $get("/api/classification_information/get_obj?classification_name=" + v).then((res) => {
        if (res.result && res.result.obj) {
                                                                                                                                                                                                                form.value.description_and_explanation = res.result.obj.description_and_explanation;
                                                                                                                                                    } else {
          console.error(res.error);
        }
      });
    };
                                                                                /**
     * 上传用户照片
     * @param {Object} param 文件参数
     */
    let upload_user_photo=(param) =>{
        uploadFile(param.file, "user_photo");
    }
                                                                    // 上传文件
    let uploadUserAvatar = (param) => {
      uploadUserFile(param.file, "avatar");
    };
    /**
     * 上传头像
     * @param {Object} file 文件对象
     * @param {key} 保存键名
     */
    const uploadUserFile = (file, key = "img") => {
      console.log(file);
      var formobj = new FormData(); // FormData 对象
      formobj.append("file", file); // 文件对象
            $upload(url_upload.value, formobj, function (json) {
        if (json.result) {
          form_user.value[key] = json.result.url;
        } else {
          ElMessage.error("上传失败！");
        }
      });
    };
        /**
    * 上传封面图
    * @param {Object} file 文件对象
    * @param {key} 保存键名
    */
    const uploadFile = (file, key = "img") => {
        var formobj = new FormData(); // FormData 对象
        formobj.append("file", file); // 文件对象
              $upload(url_upload.value, formobj, function (json) {
          if (json.result) {
            form.value[key] = json.result.url;
          } else {
            ElMessage.error("上传失败！");
          }
        });
    };
    /**
     * 上传文件
     * @param {String} url 请求地址
     * @param {Object} body 请求参数
     * @param {Funciton} func 回调函数，可以为空，为空则采用await返回值
     * @return {Object} 返回请求结果
     */
    let $upload = async (url, body, func) => {
      url = url.replace("~/", $banseUrl);

      if (func) {
        // 如果回调函数存在, 则采用异步
              $post(url, body, {
          headers: {
            "x-auth-token": token,
            "Content-Type": "multipart/form-data",
          },
        })
            .then((res) => {
              func(res);
            })
            .catch((res) => {
              func(res);
            });
      } else {
        // 否则采用同步
        var res = await $post(url, body, {
          headers: {
            "x-auth-token": token,
            "Content-Type": "multipart/form-data",
          },
        });
        return res;
      }
    };
    let get_list_vip_level = async () => {
      let json = await $get("/api/vip_level/get_list");
      if (json.result && json.result.list) {
        list_user_vip_level.value = json.result.list;
      }
    };
    let vip_level_change = (valid) => {
      for (let i = 0; i < list_user_vip_level.value.length; i++) {
        if (valid == list_user_vip_level.value[i].name) {
          form_user.value.vip_discount = list_user_vip_level.value[i].discount;
          obj_user.value.vip_discount = list_user_vip_level.value[i].discount;
          break;
        }
      }
    };
  //提交前验证事件
  let submit_check = () => {
                                                                    let document_information_id_regular = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if(param.document_information && !document_information_id_regular.test(param.document_information)){
      return "身份证号格式错误"
    }
                            };
  // 点击取消
  let cancel = () => {
    router.go(-1);
  };
    // 点击提交
    let onSubmit = async () => {
    let query_user_id = route.query.user_id
    let msg = await submit_check_user(form_user.value);
    console.log(msg);
    if (msg !== null) {
      ElMessage.error(msg);
      return;
    }
      let valid = await ruleFormRef.value.validate();
      if (valid) {
        let {
          username,
          nickname,
          avatar,
          state,
          vip_discount,
		          vip_level,
          user_group,
		        } = form_user.value;
        let param = {
          username,
          nickname,
          avatar,
          user_group: obj_user.value.user_group,
          state,
		          vip_level,
          vip_discount,
		        };
        if (query_user_id) {
          // 编辑
          param["user_id"] = query_user_id;
          $post("/api/user/set?user_id=" + query_user_id, form_user.value).then((res) => {
            if (res.result) {
                $get("/api/user/get_obj", param).then((list) => {
                if (list.result && list.result.obj) {
                    $post(url_set.value+"?registered_user_id=" + form.value["registered_user_id"], form.value).then((res2) => {
                    if (res2.result) {
					  storageHelper.setUserInfo(form_user.value)
                      ruleFormRef.value.resetFields();
                      router.push("./table");
                      ElMessage.success("修改成功！");
                    }
                  });
                }
              });
            } else if (res.error) {
              ElMessage.error(res.error.message);
            }
          });
        } else {
            // 新增
            $post("/api/user/add", form_user.value).then((res) => {
            if (res.result) {
                $get("/api/user/get_obj", param).then((list) => {
                if (list.result && list.result.obj) {
                  form.value["user_id"] = list.result.obj.user_id;
                  $post("/api/" + group_table.value + "/add", form.value).then((res) => {
                        console.log("提交结果：", res);
                        if (res.result) {
						  storageHelper.setUserInfo(form_user.value)
                          ElMessage.success("添加成功");
                          ruleFormRef.value.resetFields();
                          router.push("./table");
                        } else if (res.error) {
                          var user_id = form.value["user_id"];
                                $get("/api/user/del", { user_id });
                          ElMessage.error(res.error.message);
                        }
                      }
                  );
                }
              });
            } else if (res.error) {
              ElMessage.error(res.error.message);
            }
          });
        }
      } else {
        ElMessage.error(msg);
      }
    };
    // 提交前校验
    let submit_check_user = async (param) => {
      var ret = null;

      var email_regular = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

      var { username, password, nickname, user_group, email } = param;

      if (!username) {
        ret = "账号不能为空";
      } else if (username.length > 16 || username.length < 5) {
        ret = "账号长度应为5到16个字符之间！";
      } else if (!password) {
        ret = "密码不能为空!";
      } else if (
          is_password.value &&
          (password.length > 16 || password.length < 5)
      ) {
        ret = "密码长度应为5到16个字符之间！";
      } else if ((nickname && nickname.length > 12) || nickname.length < 2) {
        ret = "昵称长度应为2个字符到12个字符之间";
      } else if (email && !email_regular.test(email)) {
        ret = "请输入正确的邮箱地址 例：test@test.com!";
      }

      else if (!user_group) {
        ret = "请选择用户组!";
      }

      var p = { username: param.username };

      var form_obj = Object.assign({}, form.value);
                  if(!ret && !form_obj["membership_card_number"]){
        ret = "会员卡号不能为空!";
      } else if (!ret && !form_obj["registered_user_id"]) {
        let res = await $get("/api/registered_user/count", {"membership_card_number": form_obj["membership_card_number"]});
        if(res.result){
          ret = "会员卡号已存在!";
        }
      }
                                                                                                                                                                                                                                                                                                  let document_information_id_regular = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      if(form_obj.document_information && !document_information_id_regular.test(form_obj.document_information)){
        return "身份证号格式错误"
      }
                                                                                                                        
      return ret;
    };
    // 提交成功后
    let submit_after = (json, func) => {
      var formObj = Object.assign({}, form_user.value);
      delete formObj.password;
      console.log("查询表单form", formObj);
      get_register(formObj);
    };
    // 获取注册表信息
    let get_register = (form) => {
        $get("/api/user/get_obj", form).then((res) => {
        console.log("注册表信息res", res);
        if (res.result && res.result.obj) {
          form.value.user_id = res.result.obj.user_id;
          submit_sub(form.value);
        } else if (res.error) {
          console.error(res.error);
          ElMessage.error(res.error.message);
        }
      });
    };
    // 提交附加信息
    let submit_sub = (form_sub) => {
      if (form_sub["registered_users_id"]) {
        // 提交事件
        delete form_sub.update_time;
        $post("/api/" + group_table.value + "/set?registered_user_id=" + form_sub["registered_user_id"],form_sub
        ).then((res) => {
          console.log("提交结果：", res);
          if (res.result) {
            ElMessage.success("修改成功");
            router.go(-2);
          } else if (res.error) {
            console.error(res.error);
            ElMessage.error("上传失败！");
          }
        });
      } else {
        // 提交事件
          $post("/api/" + group_table.value + "/add", form_sub).then((res) => {
          console.log("提交结果：", res);
          if (res.result) {
            ElMessage.success("添加成功");
            router.go(-2);
          } else if (res.error) {
            var user_id = form_sub["user_id"];
            $get("/api/user/del", { user_id });
            ElMessage.error(res.error.message);
          }
        });
      }
    };
  // 查询数组对象某个是否包含某个值,返回对应对象
  let getObj = (arr, val) => {
    let obj = arr.find((item) => item.user_id == val);
    let ret = "";
    if (obj) {
      if (obj.nickname) {
        ret = obj.nickname;
      } else {
        ret = obj.username;
      }
    }
    return ret;
  };
  //获取对象之前
  let get_obj_before = (param) => {
    var formArr = "";
                                  if(form.value && formArr){
      Object.keys(form.value).forEach(key => {
        Object.keys(formArr).forEach(dbKey => {
          if(key === dbKey){
            disabledObj.value[key+'_isDisabled'] = true;
          }
        })
      })
    }
                                                localStorage.removeItem('form')
    return param;
  }
  // js判断字段是否是时间戳格式
  let isTimestamp = (value) => {
    return /^\d{10}$/.test(value) || /^\d{13}$/.test(value);
  }
  //获取对象之后
  let get_obj_after = (json, func) => {
    if (json) {
      var user = json.result.obj;
      is_password.value = user.password ? false : true;
      var user_id = user.user_id;
            $get("/api/" + group_table.value + "/get_obj", {user_id}).then((res) => {
        if (res.result && res.result.obj) {
          var o = res.result.obj;
          delete o["create_time"];
          delete o["update_time"];
          form.value = res.result.obj;
                                                                                                                                                      if (form.value.user_tags){
                  user_tags_multiple_value = form.value.user_tags.split(",")
          }
                                                                                                                                                                                                } else if (res.error) {
          console.log(res.error);
          console.log("获取不到相关信息");
        }
      });
    }
  }
  let is_view = () => {
    var bl = user_group == "管理员";
    if (!bl) {
      bl = $check_action('/registered_user/table', 'add');
      console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
    }
    if (!bl) {
      bl = $check_action('/registered_user/table', 'set');
      console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
    }
    if (!bl) {
      bl = $check_action('/registered_user/view', 'add');
      console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
    }
    if (!bl) {
      bl = $check_action('/registered_user/view', 'set');
      console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
    }
    if (!bl) {
      bl = $check_action('/registered_user/view', 'get');
      console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
    }
    console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");
    return bl;
  }
  // 获取详情
  let get_registered_user_obj = async () => {
    let query_user_id = route.query.user_id
    let list = await $get("/api/user/get_obj?user_id=" + query_user_id);
    get_obj_before(form_user.value);
    get_obj_after(list);
    form_user.value = list.result.obj;
    obj_user.value = form_user.value;
  };
  onMounted(() => {
                      get_list_user_tags();
          get_list_user_preferences();
                            get_list_vip_level();
    let query_user_id = route.query.user_id
    if (query_user_id) {
      get_registered_user_obj();
    }else {
      get_obj_before();
      get_obj_after();
    }

  });
</script>

<style scoped lang="less">
  .diy_edit {
    width: 100%;

    .edit_registered_user_form {
      max-width: 80%;
      margin: 90px auto;

      .fn {
        width: 100%;
      }
    }

    img {
      width: 100px;
      height: 100px;
    }

    .edit_list_registered_user_sell {
      width: 80%;
      margin: 90px auto;
    }

    .footer {
      text-align: center;
    }
  }
</style>
  